package com.pai.servlet.LeaveTicket;

import com.alibaba.fastjson.JSON;
import com.pai.entity.LeaveTicket;
import com.pai.service.AppUser.AppUserService;
import com.pai.service.AppUser.AppUserServiceImpl;
import com.pai.service.LeaveTicket.LeaveTicketService;
import com.pai.service.LeaveTicket.LeaveTicketServiceImpl;
import com.pai.servlet.BaseServlet;
import com.pai.vo.ApiResult;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@WebServlet(name = "leaveTicketColServlet",urlPatterns = {"/leaveTicketCol"})
public class LeaveTicketColServlet extends BaseServlet {
    private LeaveTicketService theService;  //请假单服务层
    private AppUserService appUserService;  //系统用户服务层

    @Override
    public void init() throws ServletException {
        super.init();
        this.theService = new LeaveTicketServiceImpl();
        this.appUserService = new AppUserServiceImpl();
    }
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ApiResult apiResult = new ApiResult();

        try{
            String action =req.getParameter("action");
            switch (action){
                case "list":
                    String colId = req.getParameter("colId");
                    List<LeaveTicket> list = theService.getListCol(colId);
                    apiResult.setData(list);
                    break;
                case "detail":
                    String id = req.getParameter("id");
                    LeaveTicket itemDetail = theService.getItemDetail(id);
                    apiResult.setData(itemDetail);
                    break;
                    //获得部门选项
                case "departmentOptions":
                    List<LeaveTicket> departmentOptions = theService.getList("0");
//                    List<SelectOption> departmentOptions = new ArrayList<>();
//                    SelectOption itemOption = new SelectOption();
//                    for(int i = 0; i < listOption.size(); i ++){
//                        itemOption.setId(listOption.get(i).getId()+"");
//                        itemOption.setName(listOption.get(i).getDepName());
//                        departmentOptions.add(itemOption);
//                    }
                    apiResult.setData(departmentOptions);
                    break;
                default:
            }
        }catch (Exception e){
            e.printStackTrace();
            apiResult.setError();
        }finally {
            write(resp, apiResult);
        }
    }

    //修改
    @Override
    protected void doPut(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ApiResult apiResult = new ApiResult();
        boolean flag = false;

        String jsonString = super.getJsonString(req);
        LeaveTicket item = JSON.parseObject(jsonString, LeaveTicket.class);

        flag = theService.update(item);
//        //将部门负责人角色设置为二级部门管理员
//        if(flag){
//            flag = appUserService.updateRole(item.getPrincipalId()+"", "二级部门管理员");
//        }


        if(!flag){
            apiResult.setCode("00001");
            apiResult.setMessage("更新失败");
        }

        super.write(resp, apiResult);
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ApiResult apiResult = new ApiResult();
        boolean flag = false;

        String id = req.getParameter("id");

        flag = theService.delete(id);

        if(!flag){
            apiResult.setCode("00001");
            apiResult.setMessage("删除失败");
        }

        super.write(resp, apiResult);
    }

    //新增
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        ApiResult apiResult = new ApiResult();
        boolean flag = false;

        String jsonString = super.getJsonString(req);
        LeaveTicket item = JSON.parseObject(jsonString, LeaveTicket.class);




        String _leaveNo = theService.generateLeaveNo(new Date());
        item.setLeaveNo(_leaveNo);
        item.setCreateDate(new Date());


        try{
            flag = theService.add(item);
        }catch (Exception e){
            e.printStackTrace();
            apiResult.setError();
        }finally {
            write(resp, apiResult);
        }
    }
}
